/**
 * Copyright 2010 Creative Works, Inc.
 * Creative Works licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.sendvox.freswitch.core;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.sendvox.freswitch.util.BundleUtilities;

public class CommandException extends Exception {

	private static final long serialVersionUID = -3540251261842095801L;

	private Exception underlyingException;
	private String localizedMessage;
	private String message;

	public CommandException(Exception underlyingException,
			String localizedMessage) {
		this.underlyingException = underlyingException;
		this.localizedMessage = localizedMessage;
	}

	public CommandException(String message, String localizedMessage) {
		super(message);
		this.message = message;
		this.localizedMessage = localizedMessage;
	}

	public Exception getUnderlyingException() {
		return underlyingException;
	}

	public void printStackTrace() {
		if (underlyingException != null) {
			underlyingException.printStackTrace();
		} else {
			super.printStackTrace();
		}
	}

	public void printStackTrace(PrintStream stream) {
		if (underlyingException != null) {
			underlyingException.printStackTrace(stream);
		} else {
			super.printStackTrace(stream);
		}
	}

	public void printStackTrace(PrintWriter writer) {
		if (underlyingException != null) {
			underlyingException.printStackTrace(writer);
		} else {
			super.printStackTrace(writer);
		}
	}

	public String getLocalizedMessage() {
		if (localizedMessage == null) {
			return message;
		}
		return localizedMessage;
	}

	public String getMessage() {
		if (message == null) {
			return localizedMessage;
		}
		return message;
	}

	protected static String getBundleString(String key) {
		String value = null;
		try {
			ResourceBundle bundle = BundleUtilities.getResourceBundle(
					CommandException.class, "Bundle"); // NOI18N
			if (bundle != null) {
				value = bundle.getString(key);
			}
		} catch (MissingResourceException exc) {
		}
		return value;
	}

	public static String getLocalMessage(String key) {
		return getLocalMessage(key, null);
	}

	public static String getLocalMessage(String key, Object[] arguments) {
		String locMessage = CommandException.getBundleString(key);
		if (locMessage == null) {
			return null;
		}
		if (arguments != null) {
			locMessage = MessageFormat.format(locMessage, arguments);
		}
		return locMessage;
	}
}
